package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.common.R;
import com.itheima.domain.ProductCategory;
import com.itheima.domain.ProductCategoryAttributeRelation;
import com.itheima.service.IProductCategoryAttributeRelationService;
import com.itheima.service.IProductCategoryService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.ProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PmsProductCategoryManager {
    @DubboReference
    IProductCategoryService productCategoryService;
    @Autowired
    IProductCategoryAttributeRelationService productCategoryAttributeRelationService;


    //分页查询商品分类
    public R<PageBeanVo> findByParentIdAndPage(Long parentId,Integer pageNum,Integer pageSize) {
        PageBeanVo pageBeanVo=productCategoryService.findByParentIdAndPage(parentId,pageNum,pageSize);
        return R.success(pageBeanVo);
    }

    //添加商品分类
    public R<String> saveProductCategory(ProductCategoryVo productCategoryVo) {
        //创建ProductCategory对象
        ProductCategory productCategory=new ProductCategory();
        BeanUtil.copyProperties(productCategoryVo,productCategory);
        ProductCategory productCategory1 = productCategoryService.saveProductCategory(productCategory);

        //将分类属性关系表中相关记录删除
        productCategoryAttributeRelationService.deleteByCid(productCategory1.getId());

        //将分类对应的属性id取出，遍历
        List<Long> productAttributeIdList = productCategoryVo.getProductAttributeIdList();
        productAttributeIdList.forEach(attrId->{
            //构建新的分类属性关系对象，存入表中
            ProductCategoryAttributeRelation relation=new ProductCategoryAttributeRelation();
            relation.setProductCategoryId(productCategory1.getId());
            relation.setProductAttributeId(attrId);

            //将新对象存入关系表中
            productCategoryAttributeRelationService.create(relation);
        });

        return R.success("添加成功");
    }

    //删除商品分类
    public R<String> deleteProductCategory(Long id) {
        //先将商品分类表中的数据删除
        productCategoryService.deleteProductCategory(id);
        //后将商品分类属性关系表中对应数据删除
        productCategoryAttributeRelationService.deleteByCid(id);
        return R.success("删除成功");
    }

    //查询所有一级分类及子分类
    public R<List<ProductCategoryVo>> findTreeCategory() {
        List<ProductCategoryVo> list =productCategoryService.findTreeCategory();
        return R.success(list);
    }

    //修改导航栏显示状态
    public R<String> updateNavStatus(String ids, Integer navStatus) {
        //获取分类id集合
        List<Long> categoryIds=new ArrayList<>();
        boolean b = ids.contains(",");
        if(b){
            String[] split = ids.split(",");
            for (String id : split) {
                categoryIds.add(Long.valueOf(id));
            }
        }else{
            categoryIds.add(Long.valueOf(ids));
        }

        //遍历，通过id查询分类
        categoryIds.forEach(categoryId->{
            ProductCategory productCategory=productCategoryService.findById(categoryId);
            //修改状态
            productCategory.setNavStatus(navStatus);
            productCategoryService.updateCategory(productCategory);
        });
        return R.success("修改成功");
    }

    //修改显示状态
    public R<String> updateShowStatus(String ids, Integer showStatus) {
        //获取分类id集合
        List<Long> categoryIds=new ArrayList<>();
        boolean b = ids.contains(",");
        if(b){
            String[] split = ids.split(",");
            for (String id : split) {
                categoryIds.add(Long.valueOf(id));
            }
        }else{
            categoryIds.add(Long.valueOf(ids));
        }

        //遍历，通过id查询分类
        categoryIds.forEach(categoryId->{
            ProductCategory productCategory=productCategoryService.findById(categoryId);
            //修改状态
            productCategory.setNavStatus(showStatus);
            productCategoryService.updateCategory(productCategory);
        });
        return R.success("修改成功");
    }

    //修改商品分类
    public R<String> updateCategory(ProductCategoryVo productCategoryVo) {
        //创建ProductCategory对象
        ProductCategory productCategory=new ProductCategory();
        BeanUtil.copyProperties(productCategoryVo,productCategory);
        productCategoryService.updateCategory(productCategory);

        //将分类属性关系表中相关记录删除
        productCategoryAttributeRelationService.deleteByCid(productCategoryVo.getId());

        //将分类对应的属性id取出，遍历
        List<Long> productAttributeIdList = productCategoryVo.getProductAttributeIdList();
        productAttributeIdList.forEach(attrId->{
            //构建新的分类属性关系对象，存入表中
            ProductCategoryAttributeRelation relation=new ProductCategoryAttributeRelation();
            relation.setProductCategoryId(productCategoryVo.getId());
            relation.setProductAttributeId(attrId);

            //将新对象存入关系表中
            productCategoryAttributeRelationService.create(relation);
        });
        return R.success("修改成功");
    }

    //根据id获取商品分类
    public R<ProductCategory> findById(Long id) {
        ProductCategory productCategory = productCategoryService.findById(id);
        return R.success(productCategory);
    }
}
